package com.test2015.Swing.popup.thumbnail;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JWindow;

public class Thumbnail extends JPanel{
	private static GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
	private static GraphicsConfiguration deCfg = environment.getDefaultScreenDevice().getDefaultConfiguration();
	
	private Container comp = null;
	private BufferedImage bi = null;
	private int w = 0;
	private int h = 0;
	private int transparency = 0;
	private JWindow popupWindow = new JWindow();
	
	public Thumbnail(JComponent comp,int w,int h,int transparency){
		this.comp = comp;
		this.w = w;
		this.h = h;
		this.transparency = transparency;
		this.setPreferredSize(new Dimension(w,h));
		this.setOpaque(false);
		popupWindow.add(this);
		com.sun.awt.AWTUtilities.setWindowOpaque(popupWindow, false);
	}
	
	public Thumbnail(int w,int h,int transparency){
		this(null,w,h,transparency);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		if(comp != null){
			int compW = comp.getWidth();
			int compH = comp.getHeight();
			BufferedImage srcImg = deCfg.createCompatibleImage(compW, compH,transparency);
			Graphics gsrc = srcImg.getGraphics();
			comp.paint(gsrc);
			gsrc.dispose();
			g.drawImage(srcImg, 0,0,w, h, null);
		}
	}
	
	public void showThumbnail(Container comp,int x ,int y){
		this.comp = comp;
		popupWindow.setLocation(x, y);
		popupWindow.setVisible(true);
		popupWindow.pack();
	}
	
	public void hideThumbnail(){
		popupWindow.setVisible(false);
	}
}
